/**
 * Created by HeartFul on 2014/8/14.
 */
//通知中心页面布局
var tzzx = function () {
    $(".dt_warp").remove();
    LeftTime = 1;
    canRefresh = false;
    var dt_warp = d3.select("body").append("div").classed("dt_warp", true).style({
        height: "100%",
        "overflow": "hidden"
    });
    var db_bg = dt_warp.append("div").classed("db_bg", true).style({
        height: "100%",

        "overflow": "hidden"
    });
    var datong_header = db_bg.append("div").classed("datong_header", true);
    datong_header.append("h1").html("通知中心").style({
        position: 'relative', top: '-2px'
    });
    /*var span = db_bg.append("div").classed("span", true).style({
     position: 'relative', top: '-22px', 'text-align': 'center', 'z-index': '99', color: '#6bb5fa'
     }).html("您有 条通知");*/

    var datong_con = db_bg.append("div").classed("datong_con", true).classed("datong_con2", true).style({
        top: '3px', height: height - 110 + "px"
    }).attr("id", "con2");

    var footer = db_bg.append("div").classed("footer", true).style({
        position: 'absolute', bottom: '0px'
    });
    footer.append("div").classed("f_navone", true).attr("type", "button").append("span");
    footer.append("div").classed("f_navtwo_click", true).attr("type", "button");
    footer.append("div").classed("f_navthree", true).attr("type", "button");
    footer.append("div").classed("f_navfour", true).attr("type", "button");
    $(".f_navone").on("click", function () { //待办列表
        task();
    });

    $(".f_navthree").on("click", function () {//桌面菜单应用
        menu();
    });
    $(".f_navfour").on("click", function () {//账号管理

        zhgl();
    });

    onTzzx();
}
var onTzzx = function () {
    height = window.innerHeight;
    if (window.localStorage.task && window.localStorage.task != "0") {
        d3.select(".f_navone span").html(window.localStorage.task).style({
            "opacity": 1
        });
        if (window.localStorage.task.length >= 3)
            d3.select(".f_navone span").html("99+")
                .style({
                    "opacity": 1,
                    "width": "30px"
                });
    }
    else
        d3.select(".f_navone span").remove();
    $("body").css("height", height + "px");

    getList();
}
var getList = function () { //获取服务器数据，并展示
    window.plugins.getIntent.show(["OA", "正在获取数据"]);
    var url = serverURL + "cmcp/noticenter/findTodos"
    PostCP(url, null, function (dat) {
        //   console.log(dat);
        if (!dat.length) {
            console.log(dat);
            window.plugins.getIntent.hide();
            Prompt("您好，没有通知或公告", 1000);
            return;
        }
        window.plugins.getIntent.hide();
        drawList(dat);
    }, function () {
        window.plugins.getIntent.hide();
        Alert("获取数据失败");
    });
}
var drawList = function (dat) { //展示数据
    var ul = d3.select(".datong_con").html("").append("ul");
    var len = dat.length;
    //  $(".span").html("您有" + len + "条通知");
    for (var i = 0; i < len; i++) {
        var title = dat[i]["todoTheme"] || "";

        title = _substr(title);
        var businessType = dat[i]["businessType"] || "";
        var businessTypeText = dat[i]["businessTypeText"] || ""
        var createCardIds = dat[i]["createCardIds"] || "";
        var time = dat[i]["time"] || "";
        var ID = dat[i]["id"] || null;
        var businessId = dat[i]["businessId"] || "";

        var userId = dat[i]["userId"] || '';
        var url = dat[i]["todoURL"];
        var li = ul.append("li").classed("no_read", true).attr("ID", ID)
            .attr("title", title).attr("businessTypeText", businessTypeText)
            .attr("businessType", businessType).attr("url", url) //modify by 2014 -12-25 工作简报 增加 获取详细数据 url参数
            .attr("createCardIds", createCardIds)
            .attr("businessId", businessId).attr("userId", userId);  // modify by 2014-12-25增加userId


        var tzImage = "../../common/page/images/tzgg.png";
        if (businessTypeText.indexOf("动态") > -1) {
            tzImage = "../../common/page/images/gzdt.png";
        }
        // 工作简报图片
        if (businessType == "0040") {
            tzImage = "../../common/page/images/gzjb.jpg";
        } // modify by 2014-12-25 工作简报图片

        li.append("dl").append("p").style("margin-right", "15px")
            .html('<img src=' + tzImage + ' width="60" height="60">')
        li.select("dl").append("dt").html(title).style({
            width: width * 0.7 + "px"
        });
        li.select("dl").append("dd").append("cite").html(businessTypeText);
        li.select("dd").append("time").html(time);
        li.on("click", function () {
            if (Flag)
                return;
            Flag = true;
            setTimeout(function () {
                Flag = false;
            }, 1000);

            var render = d3.select(".dt_warp");
            var config = {
                id: $(this).attr("ID"),
                businessType: $(this).attr("businessType"),
                businessTypeText: $(this).attr("businessTypeText"),
                businessId: $(this).attr("businessId"),
                createCardIds: $(this).attr("createCardIds"),
                userId: $(this).attr("userId"), //2014- 12-25 工作简报 增加 userId
                render: render.node()
            };
            if ($(this).attr("businessType") == "0002") {
                var ml = new Clickmed.tongzhi2(config);// 通知
            }
            else if ($(this).attr("businessType") == "0040") {
                config["todoId"] = $(this).attr("ID");
                config["url"] = $(this).attr("url");
                config["div"] =$(this);  // --增加工作简报办完后删除此项
                var ml = new Clickmed.gzjb(config);// 工作简报   modify by 2014 -12-25
            }else if ($(this).attr("businessType") == "0047" || $(this).attr("businessType") == "0048") {
                config["todoId"] = $(this).attr("ID");
                config["url"] = $(this).attr("url");
                config["div"] =$(this);  
                var ml = new Clickmed.egovern(config);// 通知中心添加政务公开的详情页
            }
            else {
                config["div"] = $(this);
                var ml = new Clickmed.tongzhi(config); // 期刊

            }

            ml.init();
        })
    }
    // 滑动效果
    iScrollDiv = new iScroll("con2", {hScrollbar: false, vScrollbar: false});

}